# coding:utf8

import tornado.web
from settings import settings
import readAndWirte as rw
import pickle


class HomeHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('home.html')


class Buy(tornado.web.RequestHandler):
    """购买页面"""
    def post(self):
        buy_book_id = int(self.get_argument("book_id"))
        buy_nums = int(self.get_argument("buy_nums"))
        buyer_name = self.get_argument("buyer_name").encode('utf8')
        buyer_address = self.get_argument("buyer_address").encode('utf8')
        content = pickle.loads(rw.get_file('books'))
        for book in content:
            if book.book_id == buy_book_id:
                if book.stock < buy_nums:
                    self.write("购买数量超过了库存")
                else:
                    # 已经付款，所以stock-
                    content[content.index(book)].stock -= buy_nums

                    orders = rw.get_file('orders')
                    new_order_id = 1
                    if not orders:
                        orders = []
                        new_order_id = 1
                    else:
                        orders = pickle.loads(orders)
                        new_order_id = int(orders[len(orders)-1].id)+1
                    new_order = Order(new_order_id, buy_book_id, buyer_name, buyer_address)
                    orders.append(new_order)
                    rw.writr_content('orders', pickle.dumps(orders))
                    self.write("已经加入订单列表，请等待处理")
                break


class Search(tornado.web.RequestHandler):
    def get(self):
        key = self.get_argument("key").encode('utf8')
        print(key)
        content = pickle.loads(rw.get_file('books'))
        for book in content:
            if book.name.find(key) >= 0 or (key.isdigit() and book.book_id == int(key) )or book.author.find(key) >= 0:
                self.render("show_book.html", info=book)


class PurchaseBooks(tornado.web.RequestHandler):
    def get(self):
        books = [
            Book("test_name", 1, 'hao', 'shandong', 10.00, 100),
            Book("追风筝的人","2","[美]卡勒德·胡赛尼","上海人民出版社","29.00", 100),
            Book("小王子","3","[法]圣埃克苏佩里","人民文学出版社","22.00", 100),
            Book("围城","4","钱锺书","人民文学出版社","19.00", 100),
            Book("活着","5","余华","南海出版公司","12.00", 100),
            Book("白夜行","6","[日]东野圭吾","南海出版公司","29.80", 100),
            Book("挪威的森林","7","[日]村上春树","上海译文出版社","18.80", 100)]
        rw.writr_content('books', pickle.dumps(books))


class CheckOrders(tornado.web.RequestHandler):
    def get(self):
        orders = rw.get_file("orders")
        if not orders:
            self.write("没有订单")
        else:
            orders = pickle.loads(orders)
            # write_str = "书ID\t购买者姓名\t购买者地址<br>"
            # for order in orders:
            #     write_str += str(order)
            self.render("orders.html", orders=orders)


class Confirm(tornado.web.RequestHandler):
    def post(self):
        order_id = int(self.get_argument("order_id"))
        orders = pickle.loads(rw.get_file("orders"))
        for order in orders:
            if order.id == order_id:
                orders.remove(order)
                print("删除成功")
        rw.writr_content("orders", pickle.dumps(orders))

class Book:
    def __init__(self, name, book_id, author, publisher, price, stock):
        self.name = name
        self.book_id = int(book_id)
        self.author = author
        self.publisher = publisher
        self.price = float(price)
        self.stock = int(stock)


class Order:
    def __init__(self, a_id, book_id, name, address):
        self.id = a_id
        self.book_id = book_id
        self.buyer_name = name
        self.buyer_address = address

    def __str__(self):
        return "%s\t%s\t%s\t%s" % (self.id, self.book_id, self.buyer_name, self.buyer_address)

